// 
// $Rev:: 27            $:  Revision of last commit
// $Author:: rvhaverm@g#$:  Author of last commit
// $Date:: 2010-07-26 1#$:  Date of last commit
// 
package agents.listeners;

import com.ibm.aglet.AgletProxy;
import com.ibm.aglet.system.ContextEvent;

import agents.agents.AgentFactory;
import agents.agents.MobileServiceAgent;
import agents.aglets.AgletBridge;
import agents.core.Components;
import agents.exceptions.IllegalCodeBaseException;
import agents.interfaces.IHostEventListener;
import agents.interfaces.IMessage;
import agents.messages.AddActionMessage;
import agents.messages.GraphicsServiceStarted;

public class GraphicServiceEventListener implements IHostEventListener
{

    @Override
    public void agletActivated(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void agletArrived(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void agletCloned(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void agletCreated(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void agletDeactivated(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void agletDispatched(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void agletDisposed(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void agletResumed(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void agletReverted(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void agletStateChanged(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void agletSuspended(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void contextShutdown(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void contextStarted(ContextEvent ev)
    {
	IMessage registerMessage = new GraphicsServiceStarted(ev.getAgletContext().getHostingURL().toString());
	
	String dispatchHost = Components.Instance().getConfiguration().getMainOffice();
	try
	{
	    AgletProxy mobileAgent = AgentFactory.newMobileAgent(ev.getAgletContext());
	    
	    IMessage addActionMessage = new AddActionMessage(registerMessage, dispatchHost);
	    
	    AgletBridge.sendMessage(mobileAgent, addActionMessage.getMessage());
	    
	    MobileServiceAgent agent = (MobileServiceAgent)AgletBridge.getAglet(mobileAgent);
	    agent.start();
	}
	catch (IllegalCodeBaseException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    @Override
    public void showDocument(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

    @Override
    public void showMessage(ContextEvent ev)
    {
	// TODO Auto-generated method stub

    }

}
